<template>
    <div class="page">
      <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <el-form-item prop="partyName">
                <el-input size="small" v-model="searchForm.partyName" placeholder="甲方名称" clearable></el-input>
         </el-form-item>
         <el-form-item prop="partyPerson">
                <el-input size="small" v-model="searchForm.partyPerson" placeholder="甲方负责人" clearable></el-input>
         </el-form-item>
         <el-form-item prop="secondName">
                <el-input size="small" v-model="searchForm.secondName" placeholder="乙方名称" clearable></el-input>
         </el-form-item>
         <el-form-item prop="secondPhone">
                <el-input size="small" v-model="searchForm.secondPhone" placeholder="乙方电话" clearable></el-input>
         </el-form-item>
         <el-form-item prop="secondPerson">
                <el-input size="small" v-model="searchForm.secondPerson" placeholder="乙方负责人" clearable></el-input>
         </el-form-item>
         <!-- <el-form-item prop="ItemNumber">
                <el-input size="small" v-model="searchForm.ItemNumber" placeholder="项目编号" clearable></el-input>
         </el-form-item> -->
         <el-form-item prop="ItemName">
                <el-input size="small" v-model="searchForm.ItemName" placeholder="项目名称" clearable></el-input>
         </el-form-item>
          <el-form-item style="float: right">
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-dialog  title="导入Excel" :visible.sync="isImportCollapse">
          <el-form size="small" :inline="true" v-show="isImportCollapse"  ref="importForm">
             <el-form-item>
              <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
             </el-form-item>
             <el-form-item prop="loginName">
                <el-upload
                  class="upload-demo"
                  :action="`${this.$http.BASE_URL}/dlyrl/hr/biddingmanagement/biddingManagement/import`"
                  :on-success="uploadSuccess"
                   :show-file-list="true">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
                </el-upload>
            </el-form-item>
          </el-form>
      </el-dialog>
      <div class="bg-white top">
      <el-row>
        <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>
        <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button>
        <el-button type="success" size="small" icon="el-icon-edit" @click="editStatus = true"
                  :disabled="dataListSelections.length <= 0" plain>修改状态
        </el-button>
        <el-button type="info" size="small" icon="el-icon-connection" @click="editService = true"
                  :disabled="dataListSelections.length <= 0" plain>投标服务费
        </el-button>
        <el-button-group class="pull-right">
            <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse"></el-button>
            <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
       size="small"
       height="calc(100% - 80px)"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      :cell-style="cellStyle" style="border:1px solid #d6cfe2;"
      :header-cell-class-name="cellHeadStyle" 
      :row-class-name="tableRowClassName"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
    <el-table-column
        prop="partyName"
        show-overflow-tooltip 
        fixed="left"
        label="甲方名称"
        width="120px">
            <!-- <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:edit')" @click="edit(scope.row.id)">{{scope.row.partyName}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:view')"  @click="view(scope.row.id)">{{scope.row.partyName}}</el-link>
              <span v-else>{{scope.row.partyName}}</span>
            </template> -->
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" @click="view(scope.row.id)">{{scope.row.partyName}}</el-link>
            </template>
      </el-table-column>
    <el-table-column
        prop="partyPerson"
        show-overflow-tooltip 
        label="甲方负责人"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="partyPhone"
        show-overflow-tooltip 
        label="甲方电话"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="secondName"
        show-overflow-tooltip 
        label="乙方名称"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="secondPhone"
        show-overflow-tooltip 
        label="乙方电话"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="secondPerson"
        show-overflow-tooltip 
        label="乙方负责人"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="mechanismName"
        show-overflow-tooltip 
        label="代理机构名称"
        width="130px"
        >
      </el-table-column>
    <el-table-column
        prop="mechanismPerson"
        show-overflow-tooltip 
        label="代理机构负责人"
        width="140px"
        >
      </el-table-column>
    <el-table-column
        prop="mechanismPhone"
        show-overflow-tooltip 
        label="代理机构电话"
        width="130px"
        >
      </el-table-column>
    <el-table-column
        prop="markingDate"
        show-overflow-tooltip 
        label="标数购买日期"
        width="130px"
        >
      </el-table-column>
    <el-table-column
        prop="bidOpeningDate"
        show-overflow-tooltip 
        label="开标日期"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="bidOpeningLocation"
        show-overflow-tooltip 
        label="开标地点"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="itemNumber"
        show-overflow-tooltip 
        label="项目编号"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="itemName"
        show-overflow-tooltip 
        label="项目名称"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="tenderPerson"
        show-overflow-tooltip 
        label="投标负责人"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="tenderPhone"
        show-overflow-tooltip 
        label="投标负责人电话"
        width="140px"
        >
      </el-table-column>
    <el-table-column
        prop="winningStatus"
        show-overflow-tooltip 
        label="中标状态"
        width="120px"
        >
        <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("winning_status", scope.row.winningStatus, '-') }}
        </template>
      </el-table-column>
    <el-table-column
        prop="bidBond"
        show-overflow-tooltip 
        label="投标保证金"
        width="120px"
        >
      </el-table-column>
    <el-table-column
        prop="upBidBond"
        show-overflow-tooltip 
        label="投标保证金大写"
        width="140px"
        >
      </el-table-column>
    <el-table-column
        prop="marginPaymentStatus"
        show-overflow-tooltip 
        label="保证金支付状态"
        width="140px"
        >
        <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("margin_payment_status", scope.row.marginPaymentStatus, '-') }}
        </template>
      </el-table-column>
    <el-table-column
        prop="upBiddingService"
        show-overflow-tooltip 
        label="投标服务费大写"
        width="140px"
        >
      </el-table-column>
    <el-table-column
        prop="serviceFeeStatus"
        show-overflow-tooltip 
        label="服务费支付状态"
        width="140px">
        <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("margin_payment_status", scope.row.serviceFeeStatus, '-') }}
        </template>
      </el-table-column>
    <el-table-column
        prop="biddingService"
        show-overflow-tooltip 
        label="投标服务费"
        width="120px">
      </el-table-column>
    <el-table-column
        prop="remarks"
        show-overflow-tooltip 
        label="备注信息"
        width="120px">
      </el-table-column>
      <!-- <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:view')" type="text" icon="el-icon-view" size="mini" @click="view(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:edit')" type="text" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('dlyrl:hr:biddingmanagement:biddingManagement:del')" type="text"  icon="el-icon-delete" size="mini" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </div>
        <!-- 弹窗, 新增 / 修改 -->
    <BiddingManagementForm  ref="biddingManagementForm" @refreshDataList="refreshList"></BiddingManagementForm>
   <el-dialog
     title="修改状态" :visible.sync="editStatus" width="40%">
      <el-form ref="editStutes" :model="editStutes" size="small">
        <el-row type="flex">
            <el-col :span="12">
                <el-form-item prop="status" label="需要修改的状态：" label-width="130px">
                  <el-select
                          v-model="editStutes.status"
                          placeholder="请选择"
                          @change="statusChang"
                          style="width:100%">
                      <el-option v-for="item in statusList" :label="item.name"
                                  :value="item.id"  :key="item.id"></el-option>
                  </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12" v-if="change">
                <el-form-item prop="winning" label="中标状态：" label-width="130px" v-if="models">
                  <el-select
                          v-model="editStutes.winning"
                          placeholder="请选择"
                          style="width:100%">
                      <el-option
                            v-for="item in $dictUtils.getDictList('winning_state')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="bond" label="保证金状态：" label-width="130px" v-if="!models">
                  <el-select
                          v-model="editStutes.bond"
                          placeholder="请选择"
                          style="width:100%">
                       <el-option
                            v-for="item in $dictUtils.getDictList('margin_payment_status')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                  </el-select>
                </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="editStatus = false">取 消</el-button>
        <el-button size="small" type="primary" @click="statusOk()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
     title="投标服务费" :visible.sync="editService" width="40%">
      <el-form ref="editStutes" :model="editStutes" size="small">
        <el-row type="flex">
            <el-col :span="12">
                <el-form-item prop="service" label="投标服务费：" label-width="130px">
                 <el-input v-model="editStutes.service"
                    autocomplete="off" @change="transformation"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item prop="upBidBond" label="投标服务费大写：" label-width="130px">
                 <el-input v-model="editStutes.upBidBond"
                    autocomplete="off"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="editService = false">取 消</el-button>
        <el-button size="small" type="primary" @click="statusOk()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import tableStyles from '../../../../../utils/mixins.js'
  import BiddingManagementForm from './BiddingManagementForm'
  export default {
    mixins: [tableStyles],
    data () {
      return {
        searchForm: {
          partyName: '',
          partyPerson: '',
          secondName: '',
          secondPhone: '',
          secondPerson: '',
          ItemNumber: '',
          ItemName: ''
        },
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isImportCollapse: false,
        loading: false,
        editStatus: false,
        editService: false,
        change: false,
        statusLabel:"",
        models:false,
        editStutes:{
          status:"",
          winning:"",
          bond:"",
          service:"",
          upBidBond:"",
        },
        statusList:[
          {
            id:"1",
            name:"中标状态",
          },
          {
            id:"2",
            name:"保证金支付状态",
          },
        ]
      }
    },
    components: {
      BiddingManagementForm
    },
    activated () {
      this.refreshList()
    },
    methods: {
      //转换大小写
      transformation(money){
        //  将数字金额转换为大写金额
          var cnNums = new Array(
            "零",
            "壹",
            "贰",
            "叁",
            "肆",
            "伍",
            "陆",
            "柒",
            "捌",
            "玖"
          ); //汉字的数字
          var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
          var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
          var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
          var cnInteger = "整"; //整数金额时后面跟的字符
          var cnIntLast = "元"; //整数完以后的单位
          //最大处理的数字
          var maxNum = 999999999999999.9999;
          var integerNum; //金额整数部分
          var decimalNum; //金额小数部分
          //输出的中文金额字符串
          var chineseStr = "";
          var parts; //分离金额后用的数组，预定义
          if (money == "") {
            return "";
          }

          money = parseFloat(money);
          if (money >= maxNum) {
            //超出最大处理数字
            return "超出最大处理数字";
          }
          if (money == 0) {
            chineseStr = cnNums[0] + cnIntLast + cnInteger;
            return chineseStr;
          }

          //四舍五入保留两位小数,转换为字符串
          money = Math.round(money * 100).toString();
          integerNum = money.substr(0, money.length - 2);
          decimalNum = money.substr(money.length - 2);

          //获取整型部分转换
          if (parseInt(integerNum, 10) > 0) {
            var zeroCount = 0;
            var IntLen = integerNum.length;
            for (var i = 0; i < IntLen; i++) {
              var n = integerNum.substr(i, 1);
              var p = IntLen - i - 1;
              var q = p / 4;
              var m = p % 4;
              if (n == "0") {
                zeroCount++;
              } else {
                if (zeroCount > 0) {
                  chineseStr += cnNums[0];
                }
                //归零
                zeroCount = 0;
                chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
              }
              if (m == 0 && zeroCount < 4) {
                chineseStr += cnIntUnits[q];
              }
            }
            chineseStr += cnIntLast;
          }
          //小数部分
          if (decimalNum != "") {
            var decLen = decimalNum.length;
            for (var i = 0; i < decLen; i++) {
              var n = decimalNum.substr(i, 1);
              if (n != "0") {
                chineseStr += cnNums[Number(n)] + cnDecUnits[i];
              }
            }
          }
          if (chineseStr == "") {
            chineseStr += cnNums[0] + cnIntLast + cnInteger;
          } else if (decimalNum == "" || /^0*$/.test(decimalNum)) {
            chineseStr += cnInteger;
          }
          console.log(chineseStr);
          this.editStutes.upBidBond = chineseStr;
          return chineseStr;
        
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/dlyrl/hr/biddingmanagement/biddingManagement/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            'company':this.$store.state.user.companyId,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.biddingManagementForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.biddingManagementForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.biddingManagementForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/dlyrl/hr/biddingmanagement/biddingManagement/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/dlyrl/hr/biddingmanagement/biddingManagement/import/template')
      },
      exportExcel () {
        let params = {
          ...this.searchForm
        }
        this.$utils.download('/dlyrl/hr/biddingmanagement/biddingManagement/export', params)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      //修改状态
      statusChang(e){
        console.log(e);
        if(e == 1){
          this.models = true;
          this.change = true;
        } else if (e == 2){
          this.models = !true;
          this.change = true;
        }
      },
      statusOk(){
        var id = this.dataListSelections[0].id;
        this.$http({
            url: '/dlyrl/hr/biddingmanagement/biddingManagement/updateStatus',
            method: 'post',
            params: {
                "id":id,
                "winningStatus": this.editStutes.winning,
                "marginPaymentStatus": this.editStutes.bond,
                "biddingService": this.editStutes.service,
                "upBiddingService": this.editStutes.upBidBond,
            }
        }).then(({data}) => {
            if (data && data.success) {
                this.$message.success(data.msg);
                this.editService = false;
                this.editStatus = false;
                this.refreshList();
                this.editStutes.status = "";
                this.editStutes.winning = "";
                this.editStutes.bond = "";
                this.editStutes.service = "";
                this.editStutes.upBidBond = "";
            }
        })
      },
    }
  }
</script>